include('shared.lua')

local FlashLvl = 0

function ENT:Initialize()
	self.ExplodeTime = CurTime() + 3
end

function ENT:FlashMe()
	local ply = LocalPlayer()
	local tracedata = {}
	tracedata.start = self:GetPos() + Vector(0,0,32)
	tracedata.endpos = ply:GetPos() + Vector(0,0,64)
	tracedata.filter = {ply,self}
	local tr = util.TraceLine(tracedata)
	local dist = tr.StartPos:Distance(tr.HitPos)
	if (tr.HitPos - tr.StartPos):DotProduct( ply:GetAimVector() ) > 0 then
		dist = dist*1.5
	end
	if not tr.Hit and dist < 1024 then
		FlashLvl = CurTime() + math.Round((1024-dist)/100)
	end
end

function ENT:Think()
	if self.ExplodeTime < CurTime() then
		local Dl = DynamicLight( self.Entity:EntIndex() )
		if Dl then
			Dl.Pos = self.Entity:GetPos()
			Dl.r = 255
			Dl.g = 255
			Dl.b = 255
			Dl.Brightness = 5
			Dl.Decay = 2500
			Dl.size = 1000
			Dl.DieTime = CurTime()+0.7
		end
		self:FlashMe()
		self.ExplodeTime = CurTime() + 3
	end
end

local function RenderFlash()
	if FlashLvl > CurTime() then
		local flash = FlashLvl - CurTime()
		local col = Color(255,255,255,255)
		if flash < 3 then
			col.a = 255*(flash/3)
		end
		draw.RoundedBox( 0, 0, 0, ScrW(), ScrH(), col )
	end
end
hook.Add("RenderScreenspaceEffects", "RenderFlash", RenderFlash)